<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>TP3 Photo</title>
	<link rel="stylesheet" type="text/css" href="css/photo.css" />
	<script language="javascript" type="text/javascript" src="script/photo.js"></script>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="Description" content="MIF13 Programmation Web TP3. Created by XIAO Yang AND MEHAS Mehdi." />
</head>
<body>
	<div id="container">
		<h1>Photos of XY et MM!</h1>
		<?php
		/**
		* This module is used for selecting and displaying the photos.
		* Write for MIF13 TP3
		*/
		/* La connexion MySQL */
		require_once("DBconn.php");
		//echo "<p>DB connect OK!</p>";
		?>
		<form action="photo.php" method="post">
			<fieldset>
				<legend>Voir table:</legend>
				<input class="submit" type="submit" name="submit" value="Voir table photo"/>
				<input class="submit" type="submit" name="submit" value="Voir table categorie"/>
			</fieldset>
			<fieldset>
				<legend>Voir photo:</legend>
				<div>
					<div>
						<select size="4" name="categorie">
							<option selected="Selectionnez un categorie (Par default, afficher toutes les photos.)">Selectionnez un categorie (Par default, afficher toutes les photos.)</option>
							<?php
							$sqlAll="SELECT * FROM mes_categories ORDER BY categorie";
							$reqAll=mysql_query($sqlAll) or die('Erreur sql: <br />'.mysql_error());
							while($data=mysql_fetch_array($reqAll, MYSQL_NUM)){
								echo "<option>".$data[0]."</option>";
								if(!isset($_SESSION["$data[0]"])){$_SESSION["$data[0]"] = $data[2];}
					//			$_SESSION["$data[0]$data[0]"] = $data[2].$data[2];
							}?>
						</select>
					</div>
					<div>
						<input class="submit" type="submit" name="submit" value="Voir les photos"/>
						<input class="submit" type="submit" name="submit" value="Voir les photos avec Auth"/>
					</div>
				</div>
			</fieldset>
		</form>
	<div id="main">
		<?php
			aff_table();
			aff_photo();
			function aff_table(){
				if($_POST['submit']=="Voir table photo"){
					$sqlAll="SELECT * FROM mes_photos ORDER BY categorie";
					$reqAll=mysql_query($sqlAll) or die('Erreur sql: <br />'.mysql_error());
					echo "<h2>The table of [Voir table photo]</h2>";
					echo "<table>";
					while($data=mysql_fetch_array($reqAll, MYSQL_NUM)){
						echo "<tr><td>".$data[0].'</td><td>'.$data[1].'</td><td>'.$data[2].'</td><td>'.$data[3].'</td><td>'.$data[4].'</td><td>'.$data[5].'</td><td>'.$data[6]."</td></tr>";
					}
					echo "</table>";
				}
				elseif($_POST['submit']=="Voir table categorie"){
					$sqlAll="SELECT * FROM mes_categories ORDER BY categorie";
					$reqAll=mysql_query($sqlAll) or die('Erreur sql: <br />'.mysql_error());
					echo "<h2>The table of [Voir table categorie]</h2>";
					echo "<table>";
					while($data=mysql_fetch_array($reqAll, MYSQL_NUM)){
						echo "<tr><td>".$data[0].'</td><td>'.$data[1].'</td><td>'.$data[2]."</td></tr>";
					}
					echo "</table>";
				}
			}
			function showPhoto(){
				echo "<h2>Here we show the categorie: ".$_POST['categorie']."</h2>";
				$sqlAll="SELECT `mes_photos`. * , `mes_categories`.`chemin` FROM `mes_categories` , `mes_photos` WHERE (mes_categories.categorie = mes_photos.categorie AND mes_categories.categorie = '".$_POST['categorie']."') ORDER BY mes_categories.categorie";
				if($_POST['submit']=="Voir les photos" && $_POST['categorie']=="Selectionnez un categorie (Par default, afficher toutes les photos.)"){
					$sqlAll="SELECT `mes_photos`. * , `mes_categories`.`chemin` FROM `mes_categories` , `mes_photos` WHERE (mes_categories.categorie = mes_photos.categorie) ORDER BY mes_categories.categorie";
				}
				$reqAll=mysql_query($sqlAll) or die('Erreur sql: <br />'.mysql_error());
				echo "<ul id=\"photoList\">";
				while($data=mysql_fetch_array($reqAll, MYSQL_NUM)){
					echo "<li><p>".$data[2].' '.$data[5].'</p><p>'.$data[6]."</p>";
					echo "<img src=\"$data[7]$data[0]\" alt=\"$data[2]\" width=\"$data[3]px\" height=\"$data[4]px\"/></li>";
				}
				echo "<ul/>";
			}
			function aff_photo(){
				if($_POST['submit']=="Voir les photos"){
					showPhoto();
				}
				if($_POST['submit']=="Voir les photos avec Auth" && $_SESSION[$_POST['categorie']] == $_SESSION[$_POST['categorie'].$_POST['categorie']]){
					showPhoto();
				}
				elseif($_POST['submit']=="Voir les photos avec Auth" && $_SESSION[$_POST['categorie']] == $_POST['password']){
					$_SESSION[$_POST['categorie'].$_POST['categorie']]=$_SESSION[$_POST['categorie']];
					echo "<p>".$_SESSION[$_POST['categorie'].$_POST['categorie']]."</p>";
					showPhoto();
				}
				elseif($_POST['submit']=="Voir les photos avec Auth" && $_SESSION[$_POST['categorie']] != $_SESSION[$_POST['categorie'].$_POST['categorie']]){
					echo "<p>Please enter the password:</p>";
					echo "<p>".$_SESSION[$_POST['categorie'].$_POST['categorie']]."</p>";
					echo $_SESSION[$_POST['categorie'].$_POST['categorie']];
					echo "<form action=\"photo.php\" method=\"post\">";
					echo "<input type=\"password\" name=\"password\">";
					echo "<input type=\"hidden\" name=\"categorie\" value=\"".$_POST['categorie']."\">";
					echo "<input type=\"submit\" name=\"submit\" value=\"Voir les photos avec Auth\"/>";
					echo "</form>";
				}
			}
			?>
			<?php
			/* Fermeture de la connexion MySQL */
				mysql_close();
			//echo "<p>DB close OK!</p>";
			?>
		</div>
	</div>
</body>
</html>